import React, { useEffect, useRef, useState } from 'react'

function useDown(count=10) {
    const [num,setNum]=useState(count)
    const [flag,setFlag]=useState(false)
    const timer=useRef(null)
    const fn=()=>{
        setFlag(true)
        if(timer.current!==null)return
        timer.current=setInterval(()=>{
            setNum((c)=>c-1)
        },1000)
    }
    useEffect(()=>{
        if(num<=0){
            clearInterval(timer.current)
            setNum(count)
            setFlag(false)
            timer.current=null
        }
    },[num])
  return [fn,flag,num]
}

export default useDown
